package com.quasar.hpatient.module.mine_portfolio;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.quasar.hpatient.R;
import com.quasar.hpatient.api.HttpClient;
import com.quasar.hpatient.api.HttpParams;
import com.quasar.hpatient.bean.mine_file.FileBagBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import lib.quasar.base.frame.BasePresenter;
import lib.quasar.base.frame.OnModelAcceptChangeListener;
import lib.quasar.context.BaseApp;
import lib.quasar.recycler.BaseCommonSectionAdapter;
import lib.quasar.recycler.decoration.SpaceDecoration;
import lib.quasar.recycler.holder.RecyclerHolder;
import lib.quasar.recycler.manager.CrashLinearLayoutManager;
import lib.quasar.util.DimenUtil;
import okhttp3.RequestBody;

/**
 * description: 我 ==> 我的档案 ==> 档案袋
 * created by kalu on 2018/7/16 11:31
 */
public class MinePortfolioPresenter implements BasePresenter {

    private final ArrayList<FileBagBean> mDatas = new ArrayList<>();

    void initList(final MinePortfolioView view, RecyclerView recycler) {

        if (null == view || null == recycler) return;

        final BaseCommonSectionAdapter<FileBagBean> adapter = new BaseCommonSectionAdapter<FileBagBean>(mDatas, R.layout.activity_mine_portfolio_type_picture, R.layout.activity_mine_portfolio_type_section) {

            @Override
            protected void onNext(RecyclerHolder holder, FileBagBean model, int position) {

                if (model.isSection()) {
                    view.setText(holder, R.id.patient_portfolio_child_title, model.getSection());
                } else {
                    view.setImage(holder, R.id.patient_portfolio_child_image, model.getUrl());
                }
            }
        };

        View loading = LayoutInflater.from(BaseApp.getContext()).inflate(R.layout.layout_recycler_loading, null);
        adapter.setNullView(loading);

        final CrashLinearLayoutManager manager = new CrashLinearLayoutManager(BaseApp.getContext());
        SpaceDecoration decoration = new SpaceDecoration(DimenUtil.dp2px(2));
        view.setRecycler(recycler, adapter, manager, decoration);
    }

    final void fileBagList(final MinePortfolioView view, RecyclerView recycler) {

        final HashMap<String, Object> map = new HashMap<>();

        final RequestBody params = createParams(map, HttpParams.DATA_TYPE_5_2);
        final Observable observable = HttpClient.getSingleHolder().getHttpService().fileBagList(params);

        request(observable, new OnModelAcceptChangeListener<Map<String, List<FileBagBean>>>() {
            @Override
            public void modelSucc(Map<String, List<FileBagBean>> map) {

                if (null == map || map.isEmpty())
                    return;

                for (Map.Entry<String, List<FileBagBean>> entry : map.entrySet()) {
                    final String key = entry.getKey();
                    final List<FileBagBean> value = entry.getValue();

                    final FileBagBean model = new FileBagBean();
                    model.setInspection_date(key);
                    mDatas.add(model);
                    mDatas.addAll(value);
                }

                view.refreshList(recycler);
            }
        });
    }

    @Override
    public void recycler() {
    }
}
